package com.hanxing.kxcharge.module.operations.service.equipmentbrand;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.equipmentbrand.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.equipmentbrand.EquipmentBrandDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.equipmentbrand.EquipmentBrandMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 设备品牌 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class EquipmentBrandServiceImpl implements EquipmentBrandService {

    @Resource
    private EquipmentBrandMapper equipmentBrandMapper;

    @Override
    public Long createEquipmentBrand(EquipmentBrandSaveReqVO createReqVO) {
        // 插入
        EquipmentBrandDO equipmentBrand = BeanUtils.toBean(createReqVO, EquipmentBrandDO.class);
        equipmentBrandMapper.insert(equipmentBrand);
        // 返回
        return equipmentBrand.getId();
    }

    @Override
    public void updateEquipmentBrand(EquipmentBrandSaveReqVO updateReqVO) {
        // 校验存在
        validateEquipmentBrandExists(updateReqVO.getId());
        // 更新
        EquipmentBrandDO updateObj = BeanUtils.toBean(updateReqVO, EquipmentBrandDO.class);
        equipmentBrandMapper.updateById(updateObj);
    }

    @Override
    public void deleteEquipmentBrand(Long id) {
        // 校验存在
        validateEquipmentBrandExists(id);
        // 删除
        equipmentBrandMapper.deleteById(id);
    }

    private void validateEquipmentBrandExists(Long id) {
        if (equipmentBrandMapper.selectById(id) == null) {
            throw exception(EQUIPMENT_BRAND_NOT_EXISTS);
        }
    }

    @Override
    public EquipmentBrandDO getEquipmentBrand(Long id) {
        return equipmentBrandMapper.selectById(id);
    }

    @Override
    public PageResult<EquipmentBrandDO> getEquipmentBrandPage(EquipmentBrandPageReqVO pageReqVO) {
        return equipmentBrandMapper.selectPage(pageReqVO);
    }

}